@echo off
:: 启用延迟变量扩展
setlocal enabledelayedexpansion

:: 检查是否存在 pid.txt 文件
if not exist pid.txt (
    echo PID file not found. Exiting.
    pause
    exit /b
)

:: 从 pid.txt 文件中读取 PID
set /p pid=<pid.txt

:: 判断是否读取到了有效的 PID
if "%pid%"=="" (
    echo No PID found in pid.txt. Exiting.
    pause
    exit /b
)

:: 检查进程是否存在
tasklist /fi "PID eq %pid%" | findstr /i "%pid%" >nul
if %errorlevel% neq 0 (
    echo Process with PID %pid% not found. Exiting.
    pause
    exit /b
)

:: 尝试发送 SIGINT 来优雅地停止进程
echo Sending SIGINT to process with PID %pid%.
taskkill /PID %pid%

:: 检查进程是否被成功终止
tasklist /fi "PID eq %pid%" | findstr /i "%pid%" >nul
if %errorlevel% equ 0 (
    echo Failed to terminate process with PID %pid%.
    echo Force terminating process with PID %pid%.
    taskkill /F /PID %pid%
) else (
    echo Process with PID %pid% has been gracefully stopped.
)

endlocal
pause
